<?php
/**
 * @copyright
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */

declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\ScoreOrder as ScoreOrderModel;
use app\admin\validate\ScoreOrderValidate;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class ScoreOrder extends BaseController

{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new ScoreOrderModel();
        $this->uid = get_login_admin('id');
    }
    /**
     * 数据列表
     */
    public function datalist()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = [];
            $where[] = ["delete_time","=",0];
            if (!empty($param['order_no'])) {
                $where[] = ['order_no','=',$param['order_no']];
            }
            if (!empty($param['keywords'])) {
                $ids = Db::name('user')->where([['nickname','like',"%{$param['keywords']}%"]])->column('id');
                $where[] = ['uid','in',$ids];
            }
            $list = $this->model->getScoreOrderList($where,$param);
            return table_assign(0, '', $list);
        }
        else{
            $express = Db::name('express')->where('delete_time',0)->order('id desc')->select();
            View::assign('express',$express);
            return view();
        }
    }

    /**
     * 订单发货
     */
    public function send()
    {
        $param = request()->param();
        if (empty($param['id'])) {
            return json(['code'=>1,'msg'=>'参数错误']);
        }
        $order = Db::name('score_order')->where('id',$param['id'])->find();
        if ($order['status'] != 1) {
            return json(['code'=>1,'msg'=>'请勿重复提交发货']);
        }
        $express = Db::name('express')->where('id',$param['express_id'])->value('name');
        Db::name('score_order')->where('id',$param['id'])->update([
            'status' => 2,
            'is_send' => 1,
            'express_id' => $param['express_id'],
            'express' => $express,
            'express_num' => $param['express_num'],
            'send_time' => time()
        ]);
        return json(['code'=>0,'msg'=>'发货成功']);
    }

    /**
     * 添加
     */
    public function add()
    {
        if (request()->isAjax()) {
            $param = get_params();

            // 检验完整性
            try {
                validate(ScoreOrderValidate::class)->check($param);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return to_assign(1, $e->getError());
            }

            $this->model->addScoreOrder($param);
        }else{
            return view();
        }
    }


    /**
     * 编辑
     */
    public function edit()
    {
        $param = get_params();

        if (request()->isAjax()) {
            // 检验完整性
            try {
                validate(ScoreOrderValidate::class)->check($param);
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return to_assign(1, $e->getError());
            }

            $this->model->editScoreOrder($param);
        }else{
            $id = isset($param['id']) ? $param['id'] : 0;
            $detail = $this->model->getScoreOrderById($id);
            if (!empty($detail)) {
                View::assign('detail', $detail);
                return view();
            }
            else{
                throw new \think\exception\HttpException(404, '找不到页面');
            }
        }
    }


    /**
     * 查看信息
     */
    public function read()
    {
        $param = get_params();
        $id = isset($param['id']) ? $param['id'] : 0;
        $detail = $this->model->getScoreOrderById($id);
        if (!empty($detail)) {
            $member = Db::name('user')->where('id',$detail['uid'])->find();
            $detail['nickname'] = $member['nickname'];
            $detail['avatar'] = $member['avatar'];
            $detail['goods_info'] = json_decode($detail['goods_info'],true);
            if (!empty($detail['address'])) {
                $detail['address'] = json_decode($detail['address'],true);
            }

            if (!empty($detail['send_time'])) {
                $detail['send_time'] = datetime($detail['send_time']);
            }
            View::assign('detail', $detail);
            return view();
        }
        else{
            throw new \think\exception\HttpException(404, '找不到页面');
        }
    }

    /**
     * 删除
     * type=0,逻辑删除，默认
     * type=1,物理删除
     */
    public function del()
    {
        $param = get_params();
        $id = isset($param['id']) ? $param['id'] : 0;
        $type = isset($param['type']) ? $param['type'] : 0;

        $this->model->delScoreOrderById($id,$type);
    }
}
